package com.wutianyi.session;

import javax.servlet.http.HttpSession;

public abstract class AbstractSessionFactory implements SessionFactory {

	private static ThreadLocal<HttpSession> resources = new InheritableThreadLocal<HttpSession>();

	@Override
	public HttpSession getHttpSession(String sessionId) {

		HttpSession httpSession = resources.get();
		if (null == httpSession) {
			httpSession = createHttpSession(sessionId);
			resources.set(httpSession);
		}

		return httpSession;
	}

	public HttpSession getHttpSession(String sessionId, boolean create) {
		if (create) {
			return getHttpSession(sessionId);
		}
		return resources.get();
	}

	protected abstract HttpSession createHttpSession(String sessionId);

	public void remove() {
		resources.remove();
	}
}
